/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game.platformer.interfaces;
import game.platformer.Player;
import game.platformer.platformer;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
/**
 *
 * @author cow
 * 
 * Hud bar:
 * Hearts - MainMenu - black bar
 * 
 */
public final class HudScreen extends HUD {
	//VarBlock--------------------
	public static final int maxDisplayableHealth = 4;
	int heartLine;  //where hearts begin to change from full to empty
	int displayedHealth;
	//textures--------------------
	Texture hudTexturesT;
	TextureRegion menuButtonTR;
	TextureRegion heartFullTR, heartEmptyTR;
	//sprites---------------------
	Sprite menuButtonS;
	Sprite[] heartsEmptyS;
	Sprite[] heartsFullS;
	//shapes----------------------
	Rectangle bBar;
	//update handlers-------------
	HealthUpdateHandler heartUpdater;
	//----------------------------
	/**
	 * initializes all the hud elements and makes it ready to be placed.
	 * @param game
	 * @param viewport
	 */
	public HudScreen(platformer game, Camera viewport){
		super(2);
		setTouchAreaBindingEnabled(true);
		//set up the textures
		hudTexturesT = new Texture(128, 64);
		//add the TexRegions
		menuButtonTR = TextureRegionFactory.createFromAsset(hudTexturesT, game, "gfx/HudMenuButton.png", 0, 0);
		heartFullTR = TextureRegionFactory.createFromAsset(hudTexturesT, game, "gfx/heartFull.png", 0, 17);
		heartEmptyTR = TextureRegionFactory.createFromAsset(hudTexturesT, game, "gfx/heartEmpty.png", 33, 17);
		//load the texture
		game.getEngine().getTextureManager().loadTexture(hudTexturesT);
		//create the menu button------------
		menuButtonS = new Sprite(140, 0, menuButtonTR) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY){
				platformer.getScreenManager().activateMainMenuScreen();
				return true;
			}
		};
		registerTouchArea(menuButtonS);
		//create the black bar--------------
		int topX = (int) (134 + menuButtonS.getWidth());
		int botY = menuButtonTR.getHeight();
		bBar = new Rectangle(topX, 0, 720 - topX, botY);
		bBar.setColor(0, 0, 0);
		//create the hearts-----------------
		heartsEmptyS = new Sprite[]{ //2 pixels between each picture, hearts are 32 wide
				new Sprite(0, 0, heartEmptyTR), //0, 0
				new Sprite(34, 0, heartEmptyTR), //(32 * 1) + (2 * 1), 0
				new Sprite(68, 0, heartEmptyTR), //(32 * 2) + (2 * 2), 0
				new Sprite(102, 0, heartEmptyTR) //(32 * 3) + (2 * 3), 0
			};
		heartsFullS = new Sprite[]{ //2 pixels between each picture, hearts are 32 wide
				new Sprite(0, 0, heartFullTR), //0, 0
				new Sprite(34, 0, heartFullTR), //(32 * 1) + (2 * 1), 0
				new Sprite(68, 0, heartFullTR), //(32 * 2) + (2 * 2), 0
				new Sprite(102, 0, heartFullTR) //(32 * 3) + (2 * 3), 0
		};
		//add necesary update handlers------
		heartUpdater = new HealthUpdateHandler();
	}
	public void remove(){
		unregisterUpdateHandler(heartUpdater);
		this.registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed){
				detachChild(menuButtonS);
				detachChild(bBar);
				for (int i = 0; i < maxDisplayableHealth; i++) {
					detachChild(heartsEmptyS[i]);
					detachChild(heartsFullS[i]);
				}
				heartLine = 0;
				unregisterUpdateHandler(this);
			}
			public void reset(){}
		});
	}
	public void attach(){
		int playerHealth = Player.getPlayer().getHealth();
		while(heartLine < playerHealth && heartLine < maxDisplayableHealth){
			attachChild(heartsFullS[heartLine++]);
		}
		while(heartLine < maxDisplayableHealth){
			attachChild(heartsEmptyS[heartLine++]);
		}
		attachChild(menuButtonS);
		attachChild(bBar);
		registerUpdateHandler(heartUpdater);
	}

	//private functions for heart display------------------
	/**
	 * decrement the heartline ad swap the
	 * previously indicated full heart to
	 * an empty heart. It is assumed that
	 * this will be called from the update
	 * handler or that it is appropriate
	 * to remove sprites from the screen
	 */
	private void shiftHeartlineLeft(){
		heartLine--;
		this.detachChild(heartsFullS[heartLine]);
		this.attachChild(heartsEmptyS[heartLine]);		
	}
	/**
	 * opposite of shiftHearLineLeft. same
	 * assumption made
	 */
	private void shiftHeartlineRight(){
		heartLine++;
		this.detachChild(heartsEmptyS[heartLine]);
		this.attachChild(heartsFullS[heartLine]);
	}

	//update handler definitions---------------------------
	private class HealthUpdateHandler implements IUpdateHandler{
		public void onUpdate(float pSecondsElapsed){
			//update the currentlt displayed health
			int playerHealth = Player.getPlayer().getHealth();
			if(heartLine != playerHealth){
				if(heartLine > playerHealth) {
					shiftHeartlineLeft();
				} else {  //hert line < player health implied
					shiftHeartlineRight();
				}
			}
		}
		public void reset(){}
	}
}
